package com.csdj.t309.controller;

import com.csdj.t309.entity.Cityarea;
import com.csdj.t309.entity.CityareaExample;
import com.csdj.t309.entity.Driverschool;
import com.csdj.t309.entity.DriverschoolExample;
import com.csdj.t309.mapper.CityareaMapper;
import com.csdj.t309.mapper.DriverschoolMapper;
import com.csdj.t309.service.DriverschoolService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.nio.file.Paths;
import java.sql.DriverManager;
import java.util.ArrayList;
import java.util.List;

@Controller
public class DriverSchoolController {
    @Autowired
    private DriverschoolService driverschoolService;
    @ResponseBody
    @GetMapping("chaxun")
    public List chaxun(@RequestParam(required=true,defaultValue="1") String page,String id)   {
        List list1=new ArrayList();
        DriverschoolExample driverschoolExample = new DriverschoolExample();
        if(id!=null&&!id.equals("")){
            driverschoolExample.createCriteria().andCityareaidEqualTo(Integer.parseInt(id));
        }
        PageHelper.startPage(Integer.parseInt(page), 3);
        List<Driverschool> list=driverschoolService.selectByExample(driverschoolExample);
        PageInfo<Driverschool> p = new PageInfo<>(list);
        list1.add(list);
        list1.add(p);
        return list1;
    }
    @ResponseBody
    @GetMapping("xiugai")
    public String xiugai(@RequestParam(value = "id")String id,@RequestParam(value = "name")String name,
                       @RequestParam(value = "chengqu")String chengqu,@RequestParam(value = "address")String address,
                       @RequestParam(value = "phone")String phone)   {
       Driverschool driverschool=new Driverschool();
       driverschool.setId(Integer.parseInt(id));
       driverschool.setName(name);
       driverschool.setCityareaid(Integer.parseInt(chengqu));
       driverschool.setAddress(address);
       driverschool.setPhone(phone);
       if(driverschoolService.updateByPrimaryKeySelective(driverschool)>0){
           return "true";
       }else {
           return "false";
       }
    }
    @ResponseBody
    @GetMapping("cha")
    public Driverschool cha(String id)   {
        DriverschoolExample driverschoolExample = new DriverschoolExample();
            driverschoolExample.createCriteria().andIdEqualTo(Integer.parseInt(id));
        List<Driverschool> list=driverschoolService.selectByExample(driverschoolExample);
        return list.get(0);
    }
}
